﻿<?php
$edit_url	=	"?act=member_post";
if ($del_id) {
	if ($_POST['submit']) {
		$mysql->query("DELETE FROM ".$tb_prefix."upload WHERE upload_id = '".$del_id."'");
		echo "Đã xóa xong <meta http-equiv='refresh' content='0;url=".$edit_url."'>";
		exit();
	}
	?><table align=center><form method="post">Bạn có muốn xóa không ??????<br><center><input value="Có" name=submit type=submit class=submit></center></form></table><?
}
// EDIT SONGS
if($mode == 'edit') {
	if(is_numeric($id)) {
		$sql 			= "SELECT * FROM ".$tb_prefix."upload WHERE upload_id = $id";
		$query 			= $mysql->query($sql);
		if($mysql->num_rows($query)) {
		$row 			= $mysql->fetch_array($query);
		
		$song_ID 		= $id;
		$song_NAME 		= $row['upload_title'];
		$memberpost		= $row['upload_poster'];
		$user_id	 = $_SESSION['admin_id'];
		$cat_ID 		= $row['m_cat'];
		$cat_NAME 		= m_get_data('CAT',$cat_ID);

		$song_URL 		= $row['upload_url'];
		$upload_check   = $row['upload_check'];
		$song_LYRIC 	= $row['upload_lyric'];
		$action			= "?act=member_post_song&mode=edit&id=$song_ID";
		if(isset($_POST['submit'])) {
			if($_POST['song'] == "" || $_POST['url'] == "" || $upload_check == '1') {
				echo " Chưa nhập đủ thông tin ";
			}
			else { 	
				$song		 = m_htmlchars(stripslashes(trim(urldecode($_POST['song']))));
				$cat		 = $_POST['cat'];
				$url		 = m_htmlchars(stripslashes(trim(urldecode($_POST['url']))));
				$lyric		 = m_htmlchars(stripslashes(trim(urldecode($_POST['lyric']))));
				$song_ascii  = strtolower(utf8_to_ascii($song));
				$date		 = date("Y-m-d",NOW);
				$time		 = time();
				$mysql->query("INSERT INTO ".$tb_prefix."data (m_title,m_title_ascii,m_singer,m_album,m_cat,m_poster,m_lyric,m_type,m_date,m_time,m_url) 
						 VALUES ('".$song."','".$song_ascii."','-1','0','".$cat."','".$memberpost."','".$lyric."','1','".$date."','".$time."','".$url."')");
                $media_id = $mysql->insert_id();
    
                /**
                *Get friend of user and gen notification to all friend 
                */
                $time = time();
                $myID = $memberpost;
                $sql = "SELECT user_name AS 'FriendName', user_id AS 'FriendId'
                            FROM ".$tb_prefix."friend
                            INNER JOIN ".$tb_prefix."user ON ".$tb_prefix."friend.re_id = ".$tb_prefix."user.user_id
                                WHERE u_id =$myID AND re_stt = 1
                        UNION
                        SELECT user_name AS 'FriendName', user_id AS 'FriendId'
                            FROM ".$tb_prefix."friend
                            INNER JOIN ".$tb_prefix."user ON ".$tb_prefix."friend.u_id = ".$tb_prefix."user.user_id
                                WHERE re_id =$myID AND re_stt = 1
                        ORDER BY `FriendName` asc";
                $q = $mysql->query($sql);
                $myName = m_get_data('USER',$myID);
                if($mysql->num_rows($q)) {
                    while($row = $mysql->fetch_array($q)) {
                        $user_id	 	= $row['FriendId'];
                        $mysql->query("INSERT INTO ".$tb_prefix."noti(obj_id, u_id, u_action_id, u_action_name, m_id, m_name, noti_type, noti_stt, noti_time)
                                                                VALUES ('$media_id','$user_id','$myID','$myName', '$media_id', '$song', 5,1,'$time')");
                    }
                }
				$mysql->query("update ".$tb_prefix."upload set upload_check = 1 where upload_id = ".$id);
				echo "<script language='JavaScript'>{ window.parent.location='?act=member_post_song&mode=edit&id=$id' }</script>";
			}
		}
	}
	if(!$mysql->num_rows($query)) {
		die("Bài hát này chưa có trong data");
	}
}
include("templates/member_post_song.html");
}

?>